unit QExport4IniFiles;

{$I QExport4VerCtrl.inc}

interface

uses Classes, IniFiles, SysUtils, QExport4Types;

type
  TQIniFile = class(TIniFile)
  private
    FVersion: Integer;
  public
    constructor Create(FileName: String);
    constructor CreateWritingVersion(FileName: String; NumVersion: Integer);
    procedure ReadSectionValuesW(const Section: string; Strings: TqeStrings);
    function ReadStringW(const Section, Ident: string;
      const Default: QEString): QEString;
    procedure WriteStringW(const Section, Ident: string; const Value: QEString);
    procedure WriteVersionSection;
    procedure ReadVersionSection;
    property Version: Integer read FVersion write FVersion;
  end;

implementation

{ TQIniFile }
constructor TQIniFile.Create(FileName: String);
begin
  inherited Create(FileName);
  FVersion := 0;
end;

constructor TQIniFile.CreateWritingVersion(FileName: String; NumVersion: Integer);
begin
  inherited Create(FileName);
  FVersion := NumVersion;
  WriteVersionSection;
end;

procedure TQIniFile.ReadSectionValuesW(const Section: string; Strings: TqeStrings);
{$IFDEF QE_UNICODE}
var
  TempStrList: TStrings;
  i: Integer;
{$ENDIF}
begin
  {$IFDEF QE_UNICODE}
  TempStrList := TStringList.Create;
  inherited ReadSectionValues(Section, TempStrList);
  if Version >= 400 then
    for i := 0 to TempStrList.Count - 1 do
      Strings.Add(Utf8Decode(AnsiString(TempStrList[i]) ))
  else
    for i := 0 to TempStrList.Count - 1 do
      Strings.Add(TempStrList[i]);
  TempStrList.Free;
  {$ELSE}
  inherited ReadSectionValues(Section, Strings);
  {$ENDIF}
end;

function TQIniFile.ReadStringW(const Section, Ident: string;
  const Default: QEString): QEString;
begin
  {$IFDEF QE_UNICODE}
  if Version >= 400 then
    Result := Utf8Decode(AnsiString(
      inherited ReadString(Section, Ident, Default)))
  else
    Result := inherited ReadString(Section, Ident, Default);
  {$ELSE}
  inherited ReadString(Section, Ident, Default);
  {$ENDIF}
end;

procedure TQIniFile.ReadVersionSection;
begin
  Version := StrToIntDef(ReadString('IniVersion', 'Number', '0'), 0);
end;

procedure TQIniFile.WriteStringW(const Section, Ident: string;
  const Value: QEString);
begin
  {$IFDEF QE_UNICODE}
  if Version >= 400 then
    inherited WriteString(Section, Ident, string(Utf8Encode(Value)))
  else
    inherited WriteString(Section, Ident, Value);
  {$ELSE}
  inherited WriteString(Section, Ident, Value);
  {$ENDIF}
end;

procedure TQIniFile.WriteVersionSection;
begin
  WriteString('IniVersion', 'Number', IntToStr(Version));
end;

end.
